import {
    AfterContentChecked, AfterViewChecked, AfterViewInit, Component, ElementRef, Input, OnChanges, OnDestroy, OnInit,
    ViewChild
} from '@angular/core';
import { ServerSocket } from '@core/services/ws.service';
import {HttpService} from '@core/services/http.service';
@Component({
    selector: 'app-c-braininfo',
    templateUrl: './c-braininfo.component.html',
    styleUrls: ['./c-braininfo.component.less']
})
export class CBraininfoComponent implements OnInit, AfterViewChecked, AfterViewInit, OnDestroy, OnChanges, AfterContentChecked {

    @ViewChild('top') top: ElementRef;
    @ViewChild('center') center: ElementRef;
    @ViewChild('all') all: ElementRef;

    @ViewChild('answerListBox') answerListBox: ElementRef;
    @Input() data;
    @Input() socket;
    @Input() IPPORT;
    @Input() goldObj;
    @Input() isScreen = false;
    @Input() dis = false;
    @Input() p_id;
    @Input() sim_id;
    @Input() type_name;
    // 测试用
    // time;
    _value='';
    _value1=[];
    _answerList = [];
    processList =[];
    g_id;
    nodeList;
    anwList = [];
    // 正式时为父组件socket接收数据 @Input()至此组件;
    @Input()
    set answerList(value) {
        this._answerList = value;
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

    get answerList() {
        return this._answerList;
    }
    constructor(public ws: ServerSocket, public ref: ElementRef,public http: HttpService) {

    }

    ngOnInit() {
        console.log(this.p_id);
        if(!this.isScreen){
            this.getProcess();
            this._value = this.data.data.fillData.title;
            this.g_id = this.data.data.fillData.key;

            if(this.data.choose_n_id.length){
                this._value1 = this.data.choose_n_id;
                this.getNodeList(this.g_id);
            }

        }else{
            console.log(this.data.choose_n_id);
            if(this.data.choose_n_id.length){
                this.http.getBrainInfoList({target_id: this.data.choose_n_id,sim_id: this.sim_id}).subscribe(res => {
                    if(res['code']==0){
                        console.log(res)
                        this.anwList = res['list']

                    }
                })
            }
        }

    }

    ngAfterViewChecked() {
        // console.log('_________***********');
        // console.log(this.goldObj);
        // this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

    processSerilize(arr) {
        for (const value of arr) {
            if (value.type === 'group') {
                this.processList.push(value);
            }
        }
    }
    getNodeList(id){
        console.log(this.data)
        this.http.getGroupBattle({g_id: id, p_id: this.p_id,isCloud:this.type_name}).subscribe(res => {
            //console.log(res)
            this.nodeList = res['list'];
            console.log(res['list'])
        });

    }
    getProcess() {
        this.http.getProjProcess({p_id: this.p_id, isCloud: this.type_name}).subscribe(res => {
            if (res.list) {
                this.processSerilize(JSON.parse(res.list[0].p_data));
            }

        });
    }
    groupChange($event) {
        console.log($event)
        this._value1 = [];
        this.nodeList = [];
        this.data.choose_n_id = [];
        this.data.data.fillData.title = $event;

        for(let list of this.processList){
            if($event == list.props.group_text) {
                this.data.data.fillData.key = list.id
                this.getNodeList(list.id)
            }
        }
    }

    nodeChange($event){
        this.data.choose_n_id = $event
        console.log(this._value1)
    }

    ngAfterViewInit() {
        // if (!this.socket) {
        //     // 测试用
        //     this.time = setInterval(() => {
        //         this.answerList.push({
        //             answer: '回答',
        //             name: 'user'
        //         });
        //         console.log(this.answerListBox.nativeElement.offsetHeight);
        //         this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
        //     }, 5000);
        // }
    }

    ngOnDestroy() {
        // clearInterval(this.time);
    }

    ngAfterContentChecked() {
        // this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }
    ngOnChanges() {
        // this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

}
